
package com.yuan.view;

import com.yuan.graduate.R;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户协议界面
 * 
 * @author YUAN
 */
public class UserAgreementView extends FrameLayout {

    private Context mContext = null;
    private GuideLastPages guidLastPages = null;
    private ViewPager viewpagerMain = null;
    private List<View> mPagerList = null; // Tab页面列表
    private View mViewPageOne = null;
    private View mViewPageTwo = null;
    private View mViewPageThree = null;
    private ImageView mImgOne = null, mImgTwo = null, mImgThree = null, mImgFour = null;
    private PagerAdapter mPagerAdapter = null;

    public UserAgreementView(Context context) {
        super(context);
        this.mContext = context;
        inflate(getContext(), R.layout.guide, this);
        init();
    }

    /**
     * @author YuanWei 四个欢迎页 用ViewPager机制实现 可以自动切换
     */
    private void init() {
        mImgOne = (ImageView) findViewById(R.id.img_one);
        mImgTwo = (ImageView) findViewById(R.id.img_two);
        mImgThree = (ImageView) findViewById(R.id.img_three);
        mImgFour = (ImageView) findViewById(R.id.img_four);
        guidLastPages = new GuideLastPages(this.mContext);
        mViewPageOne = LayoutInflater.from(this.mContext).inflate(R.layout.guide_page_one, null);
        mViewPageTwo = LayoutInflater.from(this.mContext).inflate(R.layout.guide_page_two, null);
        mViewPageThree = LayoutInflater.from(this.mContext).inflate(R.layout.guide_page_three, null);
        mPagerList = new ArrayList<View>();
        // 第一页
        mPagerList.add(mViewPageOne);
        // 第二页
        mPagerList.add(mViewPageTwo);
        // 第三页
        mPagerList.add(mViewPageThree);
        // 最后一页
        mPagerList.add(guidLastPages.getmView());
        // 初始化 pageradapter
        initPagerAdatper();
        viewpagerMain = (ViewPager) findViewById(R.id.viewpager_main);
        viewpagerMain.setOnPageChangeListener(MyOnPageChangeListener);
        viewpagerMain.setAdapter(mPagerAdapter);
    }

    public void setCurrentPage(int index) {
        if (viewpagerMain != null) {
            viewpagerMain.setCurrentItem(index);
        }
    }

    private void initPagerAdatper() {
        /**
         * ViewPage适配器类
         */
        mPagerAdapter = new PagerAdapter() {

            @Override
            public int getCount() {
                if (mPagerList != null)
                    return mPagerList.size();
                return 0;
            }

            @Override
            public void destroyItem(View container, int position, Object object) {
                ((ViewPager) container).removeView(mPagerList.get(position));
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return (arg0 == arg1);
            }

            @Override
            public Object instantiateItem(View arg0, int arg1) {
                ((ViewPager) arg0).addView(mPagerList.get(arg1), 0);
                return mPagerList.get(arg1);
            }
        };
    }

    /**
     * ViewPage监听
     */
    private OnPageChangeListener MyOnPageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int index) {
            switch (index) {
                case 0:
                    mImgOne.setImageResource(R.drawable.point_selected);
                    mImgTwo.setImageResource(R.drawable.point_normal);
                    mImgThree.setImageResource(R.drawable.point_normal);
                    mImgFour.setImageResource(R.drawable.point_normal);
                    break;
                case 1:
                    mImgOne.setImageResource(R.drawable.point_normal);
                    mImgTwo.setImageResource(R.drawable.point_selected);
                    mImgThree.setImageResource(R.drawable.point_normal);
                    mImgFour.setImageResource(R.drawable.point_normal);
                    break;
                case 2:
                    mImgOne.setImageResource(R.drawable.point_normal);
                    mImgTwo.setImageResource(R.drawable.point_normal);
                    mImgThree.setImageResource(R.drawable.point_selected);
                    mImgFour.setImageResource(R.drawable.point_normal);
                    break;
                case 3:
                    mImgOne.setImageResource(R.drawable.point_normal);
                    mImgTwo.setImageResource(R.drawable.point_normal);
                    mImgThree.setImageResource(R.drawable.point_normal);
                    mImgFour.setImageResource(R.drawable.point_selected);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }
    };

    public boolean isJoiningUx() {

        return false;
    }

    public boolean tryGoBack() {
        return false;
    }

    public void setJoiningUx(boolean isJoin) {
        if (guidLastPages != null)
            guidLastPages.setIsChecked(isJoin);
        // mCheck.setChecked(isJoin);
    }

    /**
     * 获取按钮对象
     */
    private Button getEnsureButton() {
        if (guidLastPages != null)
            return guidLastPages.getmEnsure();
        return null;
    }

    /**
     * 给确定按钮设置监听器
     * 
     * @param listener
     */
    public void setEnsureButtonOnclickListener(View.OnClickListener listener) {
        Button button = getEnsureButton();
        if (button != null) {
            button.setOnClickListener(listener);
        }
    }

    /**
     * 给确定按钮背景
     * 
     * @param text
     */
    public void setEnsureButtonBg(int drawableId) {
        Button button = getEnsureButton();
        if (button != null) {
            button.setBackgroundResource(drawableId);
        }
    }

    public void onDestroy() {
        // TODO Auto-generated method stub
        mContext = null;
        guidLastPages = null;
        viewpagerMain = null;
        mPagerList.clear();
        mPagerList = null; // Tab页面列表
        mViewPageOne = null;
        mViewPageTwo = null;
        mViewPageThree = null;
        mImgOne = null;
        mImgTwo = null;
        mImgThree = null;
        mImgFour = null;
        mPagerAdapter = null;
    }

}
